﻿using System.Collections.Generic;
using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;
using CardPlay.Model.Primitives;

namespace CardPlay.DemoPack.Games.KlondikeVariants
{
    [Game("5A911722-7124-46A4-92F3-7B0B2FC4F457", LevelOfDifficulty.Easy, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class AgnesBernauer : AgnesSorel
    {
        public AgnesBernauer()
        {
            TotalReserves = 7;

            DealOutPattern = AnyReserve;
            TableauBuildSequence = CardFaceSequence.AlternatingColorDescendingRank;
        }

        protected int TotalReserves
        {
            get; set;
        }

        protected override IEnumerable<string> InitialPileNames
        {
            get { return base.InitialPileNames.Concat(NamesOfReserves); }
        }

        protected IEnumerable<string> NamesOfReserves
        {
            get { return StandardPileNames.Reserves.Take(TotalReserves);  }
        }

        protected override ILayoutSite CreateTableauPresentation(IPresentationContext presentation)
        {
            var reserveStyle = new VisualTraits
            {
                EmptyCue = () => EmptyPileCue.CardSite,
                Arrangement = CardArrangement.StackSquare,
                ContentFit = ContentFit.FitHeight
            };

            var reserve = presentation.AddPiles(presentation.CreateRowLayout(), NamesOfReserves, reserveStyle);
            reserve.MinimumSize = new Size(0, presentation.Metrics.CardSize.Height + presentation.Metrics.MaxSpacingDown);
            reserve.HorizontalContentAlignment = HorizontalAlignment.Center;

            var root = new GridLayout
            {
                Columns = 1,
                Rows = 2,
                //HorizontalContentAlignment = HorizontalAlignment.Center,
            };

            root.Add(reserve);
            root.Add(presentation.AddTableauPiles(this, presentation.CreateRowLayout()));
            
            return root;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealCardToEach(NamesOfReserves);
            base.PrepareDealScript(script);
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return action.PileBeingMoved.BottomCard.Rank != FoundationBaseRank;
        }
    }
}